package com.example.score.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.score.entity.Class;
import com.example.score.entity.ClassStudent;
import com.example.score.entity.Student;
import com.example.score.entity.StudentScore;
import com.example.score.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
/**
 * @author shenrui
 * @date 2021/9/27
 * @description 管理员对班级的操作
 */
@Controller
public class ClassController {

    @Autowired
    ClassService classService;
    @Autowired
    StudentService studentService;
    @Autowired
    CourseService courseService;
    @Autowired
    StudentScoreService studentScoreService;
    @Autowired
    ClassStudentService classStudentService;
    @Autowired
    ClassTeacherService classTeacherService;

    /**
     *@description 查询班级列表
     * @return String
     */
    @GetMapping("/class_list")
    public String toClassList(HttpSession session) {
        List<Class> classList = classService.list();
        for(int i = 0 ; i < classList.size() ; i++){
            classList.get(i).setCourseName(courseService.getById(classList.get(i).getCourseId()).getCourseName());
        }
        session.setAttribute("classList", classList);
        return "class_list";
    }

    /**
     *@description 跳转到班级详情页面
     * @return String
     */
    @GetMapping("/class_detail_jump/{id}")
    public String classDetailJump(@PathVariable("id") Long id,
                                    HttpSession session) {
        Long score;
        List<Student> studentList = new ArrayList<>();
        StudentScore studentScore;
        Long classId = classService.getById(id).getClassId();
        List<ClassStudent> classStudentList = classStudentService.list(new LambdaQueryWrapper<ClassStudent>().eq(ClassStudent::getClassId,classId));
        for(int i = 0 ; i < classStudentList.size() ; i++) {
            Student student = studentService.getOne(new LambdaQueryWrapper<Student>().eq(Student::getStuId, classStudentList.get(i).getStudentId()));
            studentList.add(student);
        }
        for(int i = 0 ; i < studentList.size() ; i++) {
            studentScore = studentScoreService.getOne(new LambdaQueryWrapper<StudentScore>().eq(StudentScore::getStudentId,studentList.get(i).getStuId())
                                                 .eq(StudentScore::getScoreStatus,1));
            if(studentScore != null) {
                score = studentScore.getScore();
                studentList.get(i).setScore(score);
            }
        }
        session.setAttribute("studentList",studentList);
        return "redirect:/class_detail";
    }
}
